package com.wzcl.app.dao.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.wzcl.app.dao.model.common.Base;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@Entity
@ApiModel(value = "用户表")
@EntityListeners(AuditingEntityListener.class)
@Table(name = "app_user",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = {"phone"})
        }
)
public class AppUser implements Base {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToOne(cascade = CascadeType.REMOVE)
    @ApiModelProperty(value = "用户详细信息")
    @JoinColumn(name = "user_info_id", referencedColumnName = "id")
    private UserInfo userInfo;

    @ManyToOne
    @ApiModelProperty(value = "年级")
    @JoinColumn(name = "grade_id", referencedColumnName = "id")
    private Grade grade;

    @ApiModelProperty(value = "用户在系统内的昵称")
    private String nickName;

    @ApiModelProperty(value = "用户真实姓名")
    private String realName;

    @ApiModelProperty(value = "性别")
    private Integer sex;

    @ApiModelProperty(value = "用户手机号,userName")
    private String phone;

    @ApiModelProperty(value = "创建时间")
    @CreatedDate
    private Date createTime;

    @ApiModelProperty(value = "用户数据更新时间")
    @LastModifiedDate
    private Date updateTime;

    @JSONField(serialize = false)
    @ApiModelProperty(value = "用户密码密文，使用Bcrypt加密")
    private String password;

    @ApiModelProperty(value = "用户类型")
    private Integer type;

    @ApiModelProperty(value = "是否删除")
    private Integer state;

    @ApiModelProperty(value = "用户状态")
    private Integer userState;

    @ApiModelProperty(value = "头像的文件ID")
    private String headIcon;

    @ApiModelProperty(value = "当前积分")
    private BigDecimal score;

    public AppUser(String nickName, String realName, String phone) {
        this.nickName = nickName;
        this.realName = realName;
        this.phone = phone;
    }

    public AppUser() {
    }

    @JSONField(serialize = false)
    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @ApiModelProperty(value = "权限")
    @JoinTable(name = "user_role",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    private List<AppRole> roleList;
}